package edu.ustb.efws.base.controller;

import edu.ustb.efws.BaseException;
import edu.ustb.efws.base.controller.form.ResetPasswordForm;
import edu.ustb.efws.base.controller.form.UserSaveForm;
import edu.ustb.efws.base.controller.form.UserUpdateForm;
import edu.ustb.efws.controller.vo.OkVo;
import edu.ustb.efws.controller.vo.ResultPageVo;
import edu.ustb.efws.controller.vo.ResultVo;
import edu.ustb.efws.base.domain.User;
import edu.ustb.efws.base.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 用户管理API接口
 *
 * @author TS Group
 */
@RestController
@RequestMapping("/manage/user")
@Api(value = "/manage/user", tags = "用户管理")
public class UserController {
    private final UserService service;

    @Autowired
    public UserController(UserService service) {
        this.service = service;
    }

    @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("新增用户")
    public ResultVo<User> save(@RequestBody @Valid UserSaveForm form){
        if(form.getSms() && StringUtils.isBlank(form.getTel())){
            throw new BaseException("接收报警短信必须填写手机号");
        }
        return ResultVo.success(service.save(form.toDomain()));
    }

    @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("修改用户")
    public ResultVo<User> update(@RequestBody @Valid UserUpdateForm form){
        if(form.getSms() && StringUtils.isBlank(form.getTel())){
            throw new BaseException("接收报警短信必须填写手机号");
        }
        return ResultVo.success(service.update(form.toDomain()));
    }

    @PutMapping(value = "resetPassword", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("重置密码")
    public ResultVo<OkVo> resetPassword(@RequestBody @Valid ResetPasswordForm form){
        return ResultVo.success(new OkVo(service.resetPassword(form.getId(), form.getPassword())));
    }

    @GetMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("获取用户")
    public ResultVo<User> get(@PathVariable("id")long id){
        return ResultVo.success(service.get(id));
    }

    @DeleteMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("删除用户")
    public ResultVo<OkVo> delete(@PathVariable("id")long id){
        return ResultVo.success(new OkVo(service.delete(id)));
    }

    @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("查询用户")
    public ResultPageVo<User> query(
            @ApiParam(value = "用户名") @RequestParam(required = false) String username,
            @ApiParam(value = "姓名") @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "true") @ApiParam(value = "是否查询记录数") Boolean isCount,
            @RequestParam(defaultValue = "0") @ApiParam(value = "查询页数") int page,
            @RequestParam(defaultValue = "15") @ApiParam(value = "查询每页记录数") int rows){

        return new ResultPageVo
                .Builder<>(page, rows, service.query(username, name, page * rows, rows))
                .count(isCount, () -> service.count(username, name))
                .build();
    }
}
